package com.lill.product.service.impl;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lill.common.component.model.P;
import com.lill.common.component.util.IdUtil;
import com.lill.product.dao.IProductBaseDao;
import com.lill.product.dao.IProductCarDao;
import com.lill.product.model.ProductCarModel;
import com.lill.product.service.IProductCarService;
import com.lill.system.service.ISysImportService;

/**
 * 产品车辆信息Service实现类
 *
 * @author lill
 * @date 2021-08-27 09:14:44
 */
@Service("com.lill.product.service.impl.ProductCarServiceImpl")
public class ProductCarServiceImpl implements IProductCarService {
	
    @Autowired
    private IProductCarDao productCarDao;
    @Autowired
    private IProductBaseDao productBaseDao;
    @Autowired
    private ISysImportService sysImportService;

    public ProductCarModel queryObject(String productBaseId) {
        return productCarDao.queryObject(productBaseId);
    }

    public List<ProductCarModel> queryList(Map<String, Object> map) {
        return productCarDao.queryList(map);
    }

    public int queryTotal(Map<String, Object> map) {
        return productCarDao.queryTotal(map);
    }

    public int save(ProductCarModel productCar) {
        productCar.setId(IdUtil.createIdbyUUID());
        productBaseDao.save(productCar);
        return productCarDao.save(productCar);
    }

    public int update(ProductCarModel productCar) {
    	productBaseDao.update(productCar);
        return productCarDao.update(productCar);
    }

	public P importData(Map<String, Object> baseMap, String booktable, InputStream in, boolean xlsxFormat) {
		P result=P.ok();
		int rowNumStart=0;
		List<Map<String, Object>> importDataList = sysImportService.readDataListFromExcel(rowNumStart,booktable, in, xlsxFormat);
		if(CollectionUtils.isNotEmpty(importDataList)){
			List<Map<String, Object>> importDataUseList=new ArrayList<Map<String, Object>>();
			for (Map<String, Object> mapImport : importDataList) {
				Map<String, Object> useMap=new HashMap<String, Object>();
				if(null!=mapImport.get("code")&&StringUtils.isNotEmpty(mapImport.get("code").toString().trim())){
					useMap.putAll(mapImport);
					useMap.putAll(baseMap);
					useMap.put("all_name",joinAllname(mapImport));
					useMap.put("status",1);
					useMap.put("top_classify_id",ProductCarModel.classify);
					useMap.put("id",IdUtil.createIdbyUUID());
					importDataUseList.add(useMap);
				}
			}
			if(!importDataUseList.isEmpty()){
				productCarDao.saveBatchModel(importDataUseList);
			}
		}
		return result;
	}

    private String joinAllname(Map<String, Object> mapImport){
        StringBuffer allNameBuffer=new StringBuffer("");
        if(null!=mapImport.get("car_serie")&&StringUtils.isNotEmpty(mapImport.get("car_serie").toString().trim())){
        	if(allNameBuffer.length()>0){
        		allNameBuffer.append(" ");
        	}
        	allNameBuffer.append(mapImport.get("car_serie").toString().trim());
        }
        if(null!=mapImport.get("car_model")&&StringUtils.isNotEmpty(mapImport.get("car_model").toString().trim())){
        	if(allNameBuffer.length()>0){
        		allNameBuffer.append(" ");
        	}
        	allNameBuffer.append(mapImport.get("car_model").toString().trim());
        }
        if(null!=mapImport.get("design")&&StringUtils.isNotEmpty(mapImport.get("design").toString().trim())){
        	if(allNameBuffer.length()>0){
        		allNameBuffer.append(" ");
        	}
        	allNameBuffer.append(mapImport.get("design").toString().trim());
        }
        if(null!=mapImport.get("config")&&StringUtils.isNotEmpty(mapImport.get("config").toString().trim())){
        	if(allNameBuffer.length()>0){
        		allNameBuffer.append(" ");
        	}
        	allNameBuffer.append(mapImport.get("config").toString().trim());
        }
        if(null!=mapImport.get("colour")&&StringUtils.isNotEmpty(mapImport.get("colour").toString().trim())){
        	if(allNameBuffer.length()>0){
        		allNameBuffer.append(" ");
        	}
        	allNameBuffer.append(mapImport.get("colour").toString().trim());
        }
        if(null!=mapImport.get("speed_box")&&StringUtils.isNotEmpty(mapImport.get("speed_box").toString().trim())){
        	if(allNameBuffer.length()>0){
        		allNameBuffer.append(" ");
        	}
        	allNameBuffer.append(mapImport.get("speed_box").toString().trim());
        }
        if(null!=mapImport.get("displacement")&&StringUtils.isNotEmpty(mapImport.get("displacement").toString().trim())){
        	if(allNameBuffer.length()>0){
        		allNameBuffer.append(" ");
        	}
        	allNameBuffer.append(mapImport.get("displacement").toString().trim());
        }
        if(null!=mapImport.get("power")&&StringUtils.isNotEmpty(mapImport.get("power").toString().trim())){
        	if(allNameBuffer.length()>0){
        		allNameBuffer.append(" ");
        	}
        	allNameBuffer.append(mapImport.get("power").toString().trim());
        }
        return allNameBuffer.toString();
    }
}
